package com.od.service.serviceImpl;

import com.od.entity.Occupation;
import com.od.mapper.OccupationMapper;
import com.od.service.OccupationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Service
public class OccupationServiceImpl implements OccupationService {
    @Autowired(required = false)
    private OccupationMapper occupationMapper;

    @Override
    public List<Map<String, Object>> getAllOccupationByEnterpriseId(String OccupationEnterpriseId) {
        return occupationMapper.selectAllOccupationByEnterpriseId(Integer.parseInt(OccupationEnterpriseId));
    }

    @Override
    public int addOccupation(Occupation occupation) {

        return occupationMapper.addOccupation(occupation);
    }

    @Override
    public int upOccupation(Occupation occupation) {
        return occupationMapper.updateOccupationInfo(occupation);
    }

    @Override
    public Occupation getOccupationByOccupationId(Integer occupationId) {
        return occupationMapper.selectOccupationByOccupationId(occupationId);
    }

    @Override
    public int deOccupationByOccupationId(Integer occupationId) {
        return occupationMapper.deleteOccupationById(occupationId);
    }

    @Override
    public List<Map<String, Object>> getAllOccupations() {
        return occupationMapper.selectAllOccupations();
    }

    @Override
    public Integer getOccupationCount() {
        return occupationMapper.occupationCount();
    }

    @Override
    public Map<String, Object> getOccupationDetail(String occupationId) {
        return occupationMapper.selectOccupationDetial(Integer.parseInt(occupationId));
    }

    @Override
    public List<Map<String, Object>> getSearchedOccupations(Map<String, Object> data) {
        // String seekerInput = data.get("seekerInput").toString();
        // String city = data.get("city").toString();
        // String province = data.get("province").toString();
        // String salary = data.get("salary").toString();
        // String degree = data.get("degree").toString();
        // String experience = data.get("experience").toString();
        // String category = data.get("category").toString();
        // String nature = data.get("nature").toString();
        // String scale = data.get("scale").toString();
        List<Map<String, Object>> occupations = occupationMapper.selectAllOccupations();
        System.out.println(filterOccupations(occupations, data).size());
        return filterOccupations(occupations, data);
    }

    public List<Map<String, Object>> filterOccupations(List<Map<String, Object>> occupations,
            Map<String, Object> filters) {
        return occupations.stream()
                .filter(occupation -> {
                    boolean matches = true;
                    if (filters.containsKey("seekerInput")) {
                        String seekerInput = filters.get("seekerInput").toString();
                        String regex = ".*" + seekerInput.toLowerCase().chars()
                                .mapToObj(c -> (char) c)
                                .map(c -> Pattern.quote(c.toString()))
                                .collect(Collectors.joining(".*")) + ".*";
                        Pattern pattern = Pattern.compile(regex);
                        matches &= occupation.get("occupationName").toString().toLowerCase().matches(pattern.pattern());
                    }
                    if (filters.containsKey("city")) {
                        String city = filters.get("city").toString();
                        matches &= occupation.get("occupationCity").toString().equals(city) || "0".equals(city);
                    }
                    if (filters.containsKey("province")) {
                        String province = filters.get("province").toString();
                        matches &= occupation.get("occupationCity").toString().equals(province)
                                || "0".equals(province);
                    }
                    if (filters.containsKey("salary")) {
                        String salary = filters.get("salary").toString();
                        double occupationSalary = Double.parseDouble(occupation.get("occupationSalary").toString());
                        if ("0".equals(salary)) {
                            matches &= true;
                        } else if ("1".equals(salary)) {
                            matches &= occupationSalary < 3000.0;
                        } else if ("2".equals(salary)) {
                            matches &= occupationSalary >= 3000.0 && occupationSalary < 5000.0;
                        } else if ("3".equals(salary)) {
                            matches &= occupationSalary >= 5000.0 && occupationSalary < 10000.0;
                        } else if ("4".equals(salary)) {
                            matches &= occupationSalary >= 10000.0 && occupationSalary < 20000.0;
                        } else if ("5".equals(salary)) {
                            matches &= occupationSalary >= 20000.0 && occupationSalary < 40000.0;
                        } else if ("6".equals(salary)) {
                            matches &= occupationSalary >= 40000.0 && occupationSalary < 60000.0;
                        } else if ("7".equals(salary)) {
                            matches &= occupationSalary >= 60000.0;
                        }
                    }
                    if (filters.containsKey("degree")) {
                        int degree = Integer.parseInt(filters.get("degree").toString());
                        matches &= Integer.parseInt(occupation.get("occupationDegree").toString()) <= degree
                                || degree == 0;
                    }
                    if (filters.containsKey("experience")) {
                        String experience = filters.get("experience").toString();
                        int occupationExperience = Integer.parseInt(occupation.get("occupationExperience").toString());
                        if ("0".equals(experience)) {
                            matches &= true;
                        } else if ("1".equals(experience)) {
                            matches &= occupationExperience == -1;
                        } else if ("2".equals(experience)) {
                            matches &= occupationExperience <= 1;
                        } else if ("3".equals(experience)) {
                            matches &= occupationExperience >= 1 && occupationExperience <= 3;
                        } else if ("4".equals(experience)) {
                            matches &= occupationExperience >= 3 && occupationExperience <= 5;
                        } else if ("5".equals(experience)) {
                            matches &= occupationExperience >= 5 && occupationExperience <= 7;
                        } else if ("6".equals(experience)) {
                            matches &= occupationExperience >= 7 && occupationExperience <= 10;
                        } else if ("7".equals(experience)) {
                            matches &= occupationExperience >= 10;
                        }
                    }
                    if (filters.containsKey("category")) {
                        String category = filters.get("category").toString();
                        matches &= occupation.get("occupationCategory").toString().equals(category)
                                || "0".equals(category);
                    }
                    if (filters.containsKey("nature")) {
                        String nature = filters.get("nature").toString();
                        matches &= occupation.get("enterpriseNature").toString().equals(nature) || "0".equals(nature);
                    }
                    if (filters.containsKey("scale")) {
                        String scale = filters.get("scale").toString();
                        int enterpriseScale = Integer.parseInt(occupation.get("enterpriseScale").toString());
                        if ("0".equals(scale)) {
                            matches &= true;
                        } else if ("1".equals(scale)) {
                            matches &= enterpriseScale >= 1 && enterpriseScale <= 49;
                        } else if ("2".equals(scale)) {
                            matches &= enterpriseScale >= 50 && enterpriseScale <= 99;
                        } else if ("3".equals(scale)) {
                            matches &= enterpriseScale >= 100 && enterpriseScale <= 499;
                        } else if ("4".equals(scale)) {
                            matches &= enterpriseScale >= 500 && enterpriseScale <= 999;
                        } else if ("5".equals(scale)) {
                            matches &= enterpriseScale >= 1000 && enterpriseScale <= 2000;
                        } else if ("6".equals(scale)) {
                            matches &= enterpriseScale >= 2000 && enterpriseScale <= 5000;
                        } else if ("7".equals(scale)) {
                            matches &= enterpriseScale >= 5000 && enterpriseScale <= 10000;
                        } else if ("8".equals(scale)) {
                            matches &= enterpriseScale >= 10000 && enterpriseScale <= 50000;
                        } else if ("9".equals(scale)) {
                            matches &= enterpriseScale >= 50000;
                        }
                    }
                    return matches;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> enterpriseSearch(String input, String id) {
        List<Map<String, Object>> occupations = occupationMapper
                .selectAllOccupationByEnterpriseId(Integer.parseInt(id));

        return occupations.stream().filter(occupation -> {
            boolean matches = true;
            String regex = ".*" + input.toLowerCase().chars()
                    .mapToObj(c -> (char) c)
                    .map(c -> Pattern.quote(c.toString()))
                    .collect(Collectors.joining(".*")) + ".*";
            Pattern pattern = Pattern.compile(regex);
            matches &= occupation.get("occupationName").toString().toLowerCase().matches(pattern.pattern());

            return matches;
        }).collect(Collectors.toList());
    }

}
